﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Dapper;
using LibMS.Models;

namespace LibMS.Controllers
{
    [Authorize]
    public class ApiLibRoleController : ApiController
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetAll()
        {
            int? total;
            string message;
            var data = _unitOfWork.LibRole.QuerySp(out total, out message, StringConstant.TABLE_LIBROLE + StringConstant.SP_BASE_GET);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var sqlResult = data as List<LibRole> ?? null;
            var result = new
            {
                success = true,
                message = string.Empty,
                total = total,
                data = sqlResult
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }
}
